package gold.gold05;

import java.util.LinkedList;
import java.util.List;

/**
 *
 */
public class S1620T9键盘 {
    /**
     * 100, ascii表, 小写a从97开始
     * 逐个查看每个word是否符合num的排列也是可以的, 其实这个数字和字母的映射是有序的, 但是利用起来要写点东西
     */
    public char[] findChar;
    public List<String> getValidT9Words(String num, String[] words) {
        if(words == null || words.length == 0) return null;
        List<String> res = new LinkedList<>();
        findChar = new char[]{'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5',
                '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'};
        for(String word: words){
            if(matchNum(word, num))
                res.add(word);
        }
        return res;
    }
    public boolean matchNum(String word, String num){
        int len = word.length();
        for(int i = 0; i < len; i++){
            // 注意可以使用char做索引, 比较好用, 小写a - 97 == 0;
            if(findChar[word.charAt(i) - 97] != num.charAt(i))
                return false;
        }
        return true;
    }
}
